'use strict';
app.filter('paging',function(){      //paging 过滤器
  return function(lists,start){     //两个参数 lists 是在 html 里你ng-repeat的原始数据：
                                    //  start 也就是 paging 后面传的参数，即 currentPage*listsPerPage
    return lists.slice(start);     //将原始数据按照 start 分割
  };
});

app.controller('shopMenuCtrl', function ($scope, $http, FileUploader) {
    // 食物种类
    $scope.food_types = [
    	{
    		"id":"0",
            "name":"商务套餐",
            "type":"business"
    	},
    	{
    		"id":"1",
            "name":"下午茶系列",
            "type":"teatime"
    	},
    	{
    		"id":"2",
            "name":"西餐系列",
            "type":"westernfood"
    	},
    	{
    		"id":"3",
            "name":"咖啡",
            "type":"coffee"
    	},
    	{
    		"id":"4",
            "name":"茶水",
            "type":"tea"
    	},
    	{
    		"id":"5",
            "name":"饮品系列",
            "type":"drink"
    	}
    ];

    // 食物
    // $scope.food_menu = [
    // 	{
    // 		"id":"0",
    //         "name":"叫化鸡",
    //         "price":7.79,
    //         "detail":"pellentesque enim. Aliquam tempor",
    //         "img":"assets/images/img-1.jpg",
    //         "type":"business"
    // 	},
    // 	{
    // 		"id":"1",
    //         "name":"茄汁鱼卷",
    //         "price":7.79,
    //         "detail":"pellentesque enim. Aliquam tempor",
    //         "img":"assets/images/img-2.jpg",
    //         "type":"teatime"
    // 	},
    // 	{
    // 		"id":"2",
    //         "name":"鱼香肉丝",
    //         "price":7.79,
    //         "detail":"pellentesque enim. Aliquam tempor",
    //         "img":"assets/images/img-3.jpg",
    //         "type":"westernfood"
    // 	},
    // 	{
    // 		"id":"3",
    //         "name":"干煸冬笋",
    //         "price":7.79,
    //         "detail":"pellentesque enim. Aliquam tempor",
    //         "img":"assets/images/img-4.jpg",
    //         "type":"drink"
    // 	},
    // 	{
    // 		"id":"4",
    //         "name":"魔芋烧鸭",
    //         "price":7.79,
    //         "detail":"pellentesque enim. Aliquam tempor",
    //         "img":"assets/images/img-5.jpg",
    //         "type":"teatime"
    // 	},
    // 	{
    // 		"id":"5",
    //         "name":"锅贴鱼片",
    //         "price":7.79,
    //         "detail":"pellentesque enim. Aliquam tempor",
    //         "img":"assets/images/img-6.jpg",
    //         "type":"westernfood"
    // 	},
    // 	{
    // 		"id":"6",
    //         "name":"汉堡包",
    //         "price":7.79,
    //         "detail":"pellentesque enim. Aliquam tempor",
    //         "img":"assets/images/img-7.jpg",
    //         "type":"coffee"
    // 	},
    // 	{
    // 		"id":"7",
    //         "name":"麻辣肉丁",
    //         "price":7.79,
    //         "detail":"pellentesque enim. Aliquam tempor",
    //         "img":"assets/images/img-8.jpg",
    //         "type":"business"
    // 	},
    // 	{
    // 		"id":"8",
    //         "name":"鱼香茄饼",
    //         "price":7.79,
    //         "detail":"pellentesque enim. Aliquam tempor",
    //         "img":"assets/images/img-9.jpg",
    //         "type":"coffee"
    // 	},
    // 	{
    // 		"id":"9",
    //         "name":"冬菜肉末",
    //         "price":7.79,
    //         "detail":"pellentesque enim. Aliquam tempor",
    //         "img":"assets/images/img-10.jpg",
    //         "type":"tea"
    // 	},

    // ];

    // 接口
    $http.get('/api/getMenu').then(function(response){
        if(response.status==200){
            // debugger;
            console.log(response.data);
            $scope.food_menu = response.data.menu;
            // 分页
            $scope.dataNum =  $scope.food_menu.length;  //获得数据总个数
            $scope.pages = Math.ceil($scope.dataNum/6);         //按照每页显示3个数据，得到总页数
            $scope.pageNum = [];                                //生成页码，在 html里 ng-repeat 出来
            for(var i=0;i<$scope.pages;i++){
              $scope.pageNum.push(i);
            }
        }
        else{
            console.log(response.status);
        }
    });

    // $http.get('/api/getType').then(function(response){
    //     if(response.status==200){
    //         // debugger;
    //         console.log(response.data);
    //         $scope.food_types
    //     }
    //     else{
    //         console.log(response.status);
    //     }
    // });

    // 显示编辑上传后的图片
    $scope.edit_img = "";
    $scope.add_img = "";
    var addImgName = $('#addMenuImg');
    // var editImgName = $('#editMenuImg');

    $('#addMenuImg').on('change', function( e ){
        //e.currentTarget.files 是一个数组，如果支持多个文件，则需要遍历
        var name = e.currentTarget.files[0].name;
        $scope.$apply(function () {
         　　$scope.add_img = name;
        });
        // $scope.add_img = name;
        console.log($scope.add_img);
    });
    // $('#editMenuImg').on('change',"input[type='file']", function( e ){
    //     debugger;
    //     //e.currentTarget.files 是一个数组，如果支持多个文件，则需要遍历
    //     var name = e.currentTarget.files[0].name;
    //     $scope.$apply(function () {
    //      　　$scope.edit_img = name;
    //     });
    //     console.log($scope.edit_img);
    // });   

    $(document).on("change","#editMenuImg",function(e){//动态dom需要委托
        var name = e.currentTarget.files[0].name;
        $scope.$apply(function () {
         　　$scope.edit_img = name;
        });
        console.log($scope.edit_img);
    });

    // 控制叉号显示隐藏
    $scope.check_show = false;

    // 检测保存是修改还是新增，新增true，修改false
    $scope.check_add_edit = false;

    // 图片上传实例
    // // $scope.uploadStatus = false; //定义上传后返回的状态，成功获失败
    // var uploaderImage = $scope.uploaderImage = new FileUploader({
    //     url: '/api/imgUploadMeng',
    //     method: 'POST',
    //     queueLimit: 1,     //文件个数
    //     removeAfterUpload: true   //上传后删除文件
    //     // formData:[
    //     //     {
    //     //         loginId: "123"
    //     //     }
    //     // ]
    // });

    // 查询特定类型食物
    $scope.searchFood = function(type){
        var search_food = [];
        for (var i = 0; i < $scope.food_menu.length; i++) {
            if ($scope.food_menu[i].type == type) {
                search_food.push($scope.food_menu[i]);
            }
        }
        return search_food;
    };


    // food分类型展示Angular版
    $scope.filterType = "";
    $scope.type_food_item = [];
    $scope.showType = function(type){
        $scope.filterType = type;
        console.log($scope.filterType);
        // 显示分页
        if (type !='') {
            $scope.type_food_item = $scope.searchFood(type);
            // console.log($scope.type_food_item);

            $scope.dataNum =  $scope.type_food_item.length;
            $scope.pages = Math.ceil($scope.dataNum/6);         //按照每页显示3个数据，得到总页数
            $scope.pageNum = [];                                //生成页码，在 html里 ng-repeat 出来
            for(var i=0;i<$scope.pages;i++){
              $scope.pageNum.push(i);
            }
            $scope.currentPage = 0;
        }
        else{
            $scope.dataNum =  $scope.food_menu.length;  //获得数据总个数
            $scope.pages = Math.ceil($scope.dataNum/6);         //按照每页显示3个数据，得到总页数
            $scope.pageNum = [];                                //生成页码，在 html里 ng-repeat 出来
            for(var i=0;i<$scope.pages;i++){
              $scope.pageNum.push(i);
            }
            $scope.currentPage = 0;
        }
        
    };

    // JQuery类型变换&tab颜色变幻
    $(document).ready(function () {
        if ($('.menu-filter-items').length) {
            $('.menu-filter-list').on('click', 'li', function () {
                $('.menu-filter-list').find('.is-checked').removeClass('is-checked');
                $(this).addClass('is-checked');
            });
        }
    });

    // 分页
    $scope.currentPage = 0;                       //设置当前页是 0
    $scope.listsPerPage = 6;                      //设置每页显示 6 个

    $scope.setPage = function(num){             // 当点击页码数字时执行的函数
      $scope.currentPage = num;                 //将当前页 设置为 页码数
    }

    $scope.prevPage = function(){               //点击上一页执行的函数
        if($scope.currentPage > 0){
            $scope.currentPage--;
        }
    }
    $scope.nextPage = function(){              //点击下一页执行的函数
        if ($scope.currentPage < $scope.pages-1){
            $scope.currentPage++;
        }
    }

    // 编辑菜单
    $scope.edit_menuData = {};
    $scope.editMenu = function(index){
        var currentIndex = $scope.currentPage*6 + index;//防止换页时，index发生错位
        console.log(currentIndex);
        $scope.edit_menuData = $scope.food_menu[currentIndex];
        for (var i = 0; i < $scope.food_types.length; i++) {
            if ($scope.edit_menuData.type == $scope.food_types[i].type) {
                $scope.edit_menuData.type = $scope.food_types[i].name;
            }
        }
        $scope.edit_img = $scope.edit_menuData.img;
        console.log($scope.edit_menuData);
        $scope.change_type = $scope.edit_menuData.type;
    }



    // 删除菜单
    $scope.delete_menuData = {};
    $scope.deleteMenu = function(index){
        var currentIndex = $scope.currentPage*6 + index;//防止换页时，index发生错位
        // console.log(currentIndex);
        $scope.delete_menuData = $scope.food_menu[currentIndex];
        console.log($scope.delete_menuData.id);
        $http.delete('/api/deleteItem/'+$scope.delete_menuData.id).then(function(response){
            if(response.status==200){
                console.log(response);
            }
            else{
                console.log(response.status);
            }
        });
    }


    // 编辑菜单新增菜单类型
    $scope.change_type = [];

    // $scope.addfoodType = function(x){
    //     // console.log(x);
    //     if(x == "+新增类型") {
    //         document.getElementById("addtype").disabled = false;
    //     }
    //     else{
    //         document.getElementById("addtype").disabled = true;
    //         $scope.change_type = x;
    //         // console.log($scope.change_type);
    //     }
    // }
    
    // 保存编辑菜单
    // $scope.new_menuType = "";//菜单新增类型
    // $scope.saveEditmenu = function(data){

        


    // };

    // -----更新菜单类型----------
    // 食物类型操作并显示数组
    $scope.food_type_data = "";
    // 编辑食物类型
    $scope.editMenuType = function(){
        $scope.food_type_data = $scope.food_types;
    };
    // 删除食物类型
    $scope.deleteMenuType = function(index){
        var deleteType = $scope.food_type_data[index].id;
        $scope.food_type_data.splice(index,1);
        console.log(deleteType);
        var path = '/api/editMenu/'+deleteType.id;
        $http.delete(path,deleteType).then(function(response){
            if(response.status==200){
                console.log(response);
            }
            else{
                console.log(response.status);
            }
        });
    };
    // 添加食物类型
    $scope.addMenuType = function(){
        $scope.food_type_data.push({
            "name":$scope.new_menuType
        });
        console.log($scope.food_type_data);
        // $http.post('/api/addType',$scope.new_menuType).then(function(response){
        //     if(response.status==200){
        //         console.log(response);
        //     }
        //     else{
        //         console.log(response.status);
        //     }
        // });
    };
    // -----！更新菜单类型----------


    // --------添加新菜品---------
    $scope.addNewMenu  = function(){
        // console.log($scope.new_menuData);
        $scope.check_add_edit = true;
        // uploaderImage.uploadAll();
    };

    // ------！添加新菜品---------

    



});